package models

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:char(11);unique;not null;comment:账号"`
	Password string `gorm:"type:char(32);not null;comment:密码"`
	Nick     string `gorm:"type:varchar(20);not null;comment:昵称"`
	Status   int    `gorm:"type:tinyint;not null;comment:状态 0:正常 1:冻结 2:注销"`
}

func (u *User) Register() error {
	return DB.Debug().Create(&u).Error
}

func (u *User) Login() error {
	return DB.Debug().Where("mobile = ?", u.Mobile).Find(&u).Error
}

func (u *User) ShowUser() error {
	return DB.Debug().Where("id = ?", u.ID).Find(&u).Error
}

func (u *User) UpdateUser() error {
	return DB.Debug().Where("id = ?", u.ID).Updates(&u).Error
}

func (u *User) ListUser(status int) (user []*User, err error) {
	err = DB.Debug().Where("status = ?", status).Order("status asc").Find(&user).Error
	return
}

type AuthRealName struct {
	gorm.Model
	UserId   int    `gorm:"type:int(11);not null;comment:用户ID"`
	RealName string `gorm:"type:varchar(20);not null;comment:真实姓名"`
	IdCard   string `gorm:"type:char(18);not null;comment:身份证号"`
}

func (a *AuthRealName) AddAuthRealName() error {
	return DB.Debug().Create(&a).Error
}

func (a *AuthRealName) ShowRealNameByUserId() error {
	return DB.Debug().Where("user_id = ?", a.UserId).Find(&a).Error
}

type LikeHouse struct {
	gorm.Model
	UserId  int `gorm:"type:int(11);not null;comment:用户ID"`
	HouseId int `gorm:"type:int(11);not null;comment:房源ID"`
}

func (like *LikeHouse) AddLike() error {
	return DB.Debug().Create(&like).Error
}
